<!-- 打开app -->
<template>
  <div>
    <van-popup v-model="show" position="top" :overlay="false">
      <van-cell :title="$store.state.temp.mailInfo ? $store.state.temp.mailInfo.name : ''" title-style='text-align: left;' center>
        <template #icon>
          <van-icon size='35px' :name="$store.state.temp.mailInfo ? $store.state.temp.mailInfo.logo: ''" style="border-radius: 50%; overflow: hidden; margin-right: 8px;"/>
        </template>
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-button size="small" v-show="!fun.isWeiXin()&&!appid" type="danger" @click.stop="open">打开app</van-button>
          <wx-open-launch-app
            v-show="fun.isWeiXin()&&appid"
            id="launch-btn"
            :appid="appid"
            :extinfo="`{route: '/activity/RouterYzRouterWatchActivity',param: {roomId: ${roomid}}}`"
          >
            <script type="text/wxtag-template">
              <style>.btn { padding: 4px 10px;background:#f15353;color:#fff;border: none;}</style>
              <button class="btn">打开APP</button>
            </script>
          </wx-open-launch-app>

        </template>
      </van-cell>
    </van-popup>
    <yz-wechat-share-img v-model="showWechatshar" :text="`点击右上角'…'的按钮, 选择在浏览器中打开`"></yz-wechat-share-img>
  </div>
</template>

<script>
import {openApp,goConfirmAddr} from './open-open.js';
import yzWechatShareImg from "components/ui_components/yz_wechatShareImg";
export default {
  props: {
    value: {
      //获取父组件的数据value
      type: Boolean,
      default: false
    },
    roomid: {
      //直播间id
      type: [String,Number],
      default: '0'
    },
    appid: {
      //移动应用appid，需要在微信开放平台设置绑定
      type: [String,Number],
      default: 0
    },
  },
  data() {
    return {
      show: true,
      showWechatshar: false
    };
  },
  watch: {
    value(val) {
      this.show = val;
    }
  },

  activated() {},

  components: {yzWechatShareImg},

  computed: {},

  mounted() {
    var btn = document.getElementById('launch-btn');
    btn.addEventListener('launch', function (e) {
      console.log('success');
    });
    btn.addEventListener('error', (e)=> {
      this.$dialog.alert({
        title: '提示',
        confirmButtonText: '下载app',
        message: '未找到相关应用，微信启动APP失败',
      })
        .then(() => {
          // on confirm
          // this.open();
          goConfirmAddr();//跳转下载页
        })
        .catch(() => {
          // on cancel
        });
    });

  },

  methods: {
    open(){
      if(this.fun.isWeiXin()){
        this.showWechatshar = true;
        return;
      }
      this.$toast("正在尝试打开应用，请稍后");
      let _Domain  = window.location.host;//域名
      let _i = this.fun.getKeyByI();//平台id
      let _roomid = this.roomid;//直播间id
      let _scheme = `cpsscheme://${_Domain}:${_i}/liveInfo?roomId=${_roomid}`;
      openApp(_scheme);
    }
  }
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped></style>
